<?php
/**
 * 设置控制器包含（个人信息， 个人认证， 企业认证， 家庭成员， 工作单位信息， 分销中心， 合伙人申请， 会员升级）
 */

namespace app\index\controller;

use app\common\exception\System as SystemException;
use Exception;
use app\common\library\Upload;

class Setting extends Common
{
    /**
     * 不需要登录
     * @var array
     */
    protected $noNeedLogin = ['index', 'uplodeImg', 'setFamily'];

    /**
     * 个人信息
     */
    public function info()
    {
//        $member_id = trim($this->request->post('member_id'));
        $member_id = $this->auth->member_id;

        $res = service('index/Setting')->info($member_id);
        return $this->successReturn($res, SystemException::E_SUCCESS, '获取成功');
    }

    /**
     * @return \think\Response
     * 修改基本信息
     */
    public function editMemberInfo()
    {
        $member_id = $this->auth->member_id;
        $res = service('index/Setting')->info($member_id);
//        $member_id = trim($this->request->post('member_id'));
        if ($this->request->isPost()) {
            $param['nickname'] = trim($this->request->post('nickname'));
            $param['age'] = trim($this->request->post('age'));
            $param['sign_text'] = trim($this->request->post('sign_text'));
            $param['email'] = trim($this->request->post('email'));
            $param['sex'] = trim($this->request->post('sex'));
            $res = service('index/Setting')->editMemberInfo($member_id, $param);
            if ($res) {
                return $this->successReturn($res, SystemException::E_SUCCESS, '修改成功');
            } else {
                return $this->errorReturn(SystemException::E_VALIDATE, '修改失败');
            }
        }
        return $this->successReturn($res, SystemException::E_SUCCESS, '修改成功');
    }


    /**
     * 修改用户发票信息
     */
    public function editMemberInvoice()
    {
        $member_id = $this->auth->member_id;
//        $member_id = trim($this->request->get('member_id'));
        $res = service('index/Setting')->getMemberInvoice($member_id);
        if ($this->request->isPost()) {
            $param['type'] = trim($this->request->post('type'));
            $param['header'] = trim($this->request->post('header'));
            $param['tax_no'] = trim($this->request->post('tax_no'));
            $param['is_duty'] = trim($this->request->post('is_duty'));
            $param['reg_address'] = trim($this->request->post('reg_address'));
            $param['phone'] = trim($this->request->post('phone'));
            $param['open_bank'] = trim($this->request->post('open_bank'));
            $param['bank_no'] = trim($this->request->post('bank_no'));
            $param['is_default'] = trim($this->request->post('is_default'));
            $param['create_time'] = time();
            $res = service('index/Setting')->editMemberInvoice($member_id, $param);
            return $this->successReturn($res, SystemException::E_SUCCESS, '修改成功');
        }
        return $this->successReturn($res, SystemException::E_SUCCESS, '获取成功');
    }


    /**
     * @return \think\Response
     * 上传图片
     */
    public function uplodeImg()
    {
        $file = $this->request->file('file');
        try {
            $res = Upload::image($file);
            $url = 'http://' . $_SERVER['HTTP_HOST'] . '/uploads/image/' . $res;
            return $this->successReturn($url, SystemException::E_SUCCESS, '获取成功');
        } catch (Exception $e) {
            return $this->errorReturn($e->getMessage());
        }
    }


    /**
     * @return \think\Response
     * 设置企业认证
     */
    public function setMemberBusinessCert()
    {
        $member_id = $this->auth->member_id;
//        $member_id = $this->request->get('member_id');
        $cert = service('index/Setting')->getMemberBusinessCert($member_id);
        if ($this->request->isPost()) {
            $param['member_id'] = $member_id;
            $param['name'] = $this->request->post('name');
            $param['business_license'] = $this->request->post('business_license');
            $param['legal_person'] = $this->request->post('legal_person');
            $param['card_front'] = $this->request->post('card_front');
            $param['card_back'] = $this->request->post('card_back');
            $param['bank_no'] = $this->request->post('bank_no');
            $param['letter_attorney'] = $this->request->post('letter_attorney');
            $param['is_treaty'] = $this->request->post('is_treaty');
            $param['is_verfiy'] = $this->request->post('is_verfiy');
            $param['create_time'] = time();
            $res = service('index/Setting')->setMemberBusinessCert($member_id, $param);
            if ($res) {
                return $this->successReturn($cert, SystemException::E_SUCCESS, '成功');
            } else {
                return $this->errorReturn(SystemException::E_VALIDATE, '失败');
            }
        }
        return $this->successReturn($cert, SystemException::E_SUCCESS, '获取成功');
    }


    /**
     * @return \think\Response
     * 设置个人认证
     */
    public function setMemberPersonalCert()
    {
        $member_id = $this->auth->member_id;
//        $member_id = $this->request->get('member_id');
        $cert = service('index/Setting')->getMemberPersonalCert($member_id);
        if ($this->request->isPost()) {
            $param['member_id'] = $member_id;
            $param['nickname'] = $this->request->post('nickname');
            $param['id_card'] = $this->request->post('id_card');
            $param['card_front'] = $this->request->post('card_front');
            $param['card_back'] = $this->request->post('card_back');
            $param['bank_no'] = $this->request->post('bank_no');
            $param['is_verfiy'] = $this->request->post('is_verfiy');
            $param['create_time'] = time();
            $res = service('index/Setting')->setMemberPersonalCert($member_id, $param);
            if ($res) {
                return $this->successReturn($cert, SystemException::E_SUCCESS, '成功');
            } else {
                return $this->errorReturn(SystemException::E_VALIDATE, '失败');
            }
        }
        return $this->successReturn($cert, SystemException::E_SUCCESS, '获取成功');
    }


    /**
     * @return \think\Response
     * 设置工作单位信息
     */
    public function setMemberWorker()
    {
        $member_id = $this->auth->member_id;
//        $member_id = $this->request->get('member_id');
        $cert = service('index/Setting')->getMemberWorker($member_id);
        if ($this->request->isPost()) {
            $param['member_id'] = $member_id;
            $param['name'] = $this->request->post('name');
            $param['phone'] = $this->request->post('phone');
            $param['address'] = $this->request->post('address');
            $param['worker_one'] = $this->request->post('worker_one');
            $param['phone_one'] = $this->request->post('phone_one');
            $param['worker_two'] = $this->request->post('worker_two');
            $param['phone_two'] = $this->request->post('phone_two');
            $param['phone_two'] = $this->request->post('phone_two');
            $param['create_time'] = time();
            $res = service('index/Setting')->setMemberWorker($member_id, $param);
            if ($res) {
                return $this->successReturn($cert, SystemException::E_SUCCESS, '成功');
            } else {
                return $this->errorReturn(SystemException::E_VALIDATE, '失败');
            }
        }
        return $this->successReturn($cert, SystemException::E_SUCCESS, '获取成功');
    }


    /**
     * @return \think\Response
     * 设置家庭信息
     */
    public function setFamily()
    {
        $member_id = $this->auth->member_id;
//                $member_id = $this->request->get('member_id');

        $familyData = service('index/Setting')->getFamily($member_id);
        if ($this->request->isPost()) {
            $param['member_id'] = $member_id;
            $param['nickname'] = $this->request->post('nickname');
            $param['card_no'] = $this->request->post('card_no');
            $param['relation'] = $this->request->post('relation');
            $param['phone'] = $this->request->post('phone');
            $param['nickname_two'] = $this->request->post('nickname_two');
            $param['card_no_two'] = $this->request->post('card_no_two');
            $param['relation_two'] = $this->request->post('relation_two');
            $param['phone_two'] = $this->request->post('phone_two');
            $param['create_time'] = time();
            $res = service('index/Setting')->setFamily($member_id, $param);
            if ($res) {
                return $this->successReturn($familyData, SystemException::E_SUCCESS, '成功');
            } else {
                return $this->errorReturn(SystemException::E_VALIDATE, '失败');
            }
        }
        return $this->successReturn($familyData, SystemException::E_SUCCESS, '获取成功');
    }


    /**
     * @return \think\Response
     * 设置我的收货地址
     */
    public function setMemberAddress()
    {
        $member_id = $this->auth->member_id;
//        $member_id = $this->request->post('member_id');
        $address = service('index/Setting')->getMemberAddress($member_id);
        if ($this->request->isPost()) {
            $param['member_id'] = $member_id;
            $param['name'] = $this->request->post('name');
            $param['mobile'] = $this->request->post('mobile');
            $param['province'] = $this->request->post('province');
            $param['city'] = $this->request->post('city');
            $param['area'] = $this->request->post('area');
            $param['address'] = $this->request->post('address');
            $param['is_default'] = $this->request->post('is_default');
            $res = service('index/Setting')->setMemberAddress($member_id, $param);
            if ($res) {
                return $this->successReturn($address, SystemException::E_SUCCESS, '成功');
            } else {
                return $this->errorReturn(SystemException::E_VALIDATE, '失败');
            }
        }
        return $this->successReturn($address, SystemException::E_SUCCESS, '获取成功');
    }

    /**
     * @return mixed
     * 修改登录密码
     */
    public function resetPwd()
    {
        $data = [
            'password' => $this->request->post('password'),
            'new_password' => $this->request->post('new_password'),
        ];
        $rule = [
            'password' => 'require',
            'new_password' => 'require',
        ];

        $validate = $this->validate($data, $rule);
        if ($validate !== true) {
            return $this->errorReturn(SystemException::E_VALIDATE, $validate);
        }
        try {
            $userInfo = $this->auth->getUser();
            if (!$userInfo) {
                exception(UserException::getErrorMsg(UserException::E_PASSWORD), UserException::E_PASSWORD, $this->_exceptions['user']);
            }
            //验证旧密码
            $res = $this->auth->checkEncryptPassword($userInfo['password'], $data['password'], $userInfo['salt']);
            if (!$res)
            {
                return $this->errorReturn(SystemException::E_VALIDATE, '旧密码错误');
            }
            // 修改支付密码
            $memberModel = model('common/Members');
            $password = $this->auth->getEncryptPassword($data['new_password'], $userInfo['salt']);
            $res = $memberModel->where(['member_id' => $this->auth->member_id])->update(['password' => $password]);
        } catch (UserException $userE) {
            return $this->errorReturn($userE->getCode(), $userE->getMessage());
        }
        return $this->successReturn($res, SystemException::E_SUCCESS, '修改成功');
    }

    /**
     * 设置支付密码
     * @return \think\Response
     */
    public function setPayPassword()
    {

        if ($this->request->isPost()) {
            if (!$this->auth->member_id) {
                return $this->errorReturn(UserException::E_IS_LOGIN, UserException::$messageList[UserException::E_IS_LOGIN]);
            }
            $old_pay_password  =  trim($this->request->post('old_pay_password'));
            $is_edit  =  trim($this->request->post('is_edit'));
            $data = [
                'pay_password' => trim($this->request->post('pay_password'))
            ];
            try {
                $userInfo = $this->auth->getUser();
                if (!$userInfo) {
                    exception(UserException::getErrorMsg(UserException::E_PASSWORD), UserException::E_PASSWORD, $this->_exceptions['user']);
                }
                //验证旧支付密码
                if(!empty($old_pay_password) && $is_edit == 1){
                    $res = $this->auth->checkEncryptPassword($userInfo['pay_password'], $old_pay_password, $userInfo['salt']);
                    if (!$res)
                    {
                        return $this->errorReturn(SystemException::E_VALIDATE, '旧密码错误');
                    }
                }
                // 修改支付密码
                $memberModel = model('common/Members');
                $pay_password = $this->auth->getEncryptPassword($data['pay_password'], $userInfo['salt']);
                $res = $memberModel->where(['member_id' => $this->auth->member_id])->update(['pay_password' => $pay_password]);
            } catch (UserException $userE) {
                return $this->errorReturn($userE->getCode(), $userE->getMessage());
            }
            return $this->successReturn($res, SystemException::E_SUCCESS, '設定成功');
        }
        return $this->successReturn(['has_pay_pwd'=> !$this->auth->pay_password ? 0 : 1], SystemException::E_SUCCESS, '設定成功');
    }

    /**
     * @return mixed
     * 充值手机号
     */
    public function resetPhone()
    {
        if ($this->request->isPost()) {

            $data = [
                'phone' => $this->request->post('phone'),
            ];
            $rule = [
                'phone' => [
                    'require',
                    'max' => 11,
                    'regex' => '/^((13[0-9])|(14[5,7])|(15[0-3,5-9])|16[5]|16[6]|(17[0,3,5-8])|(18[0-9])|19[89])\d{8}$/',
                    //  'unique:members',
                ],
            ];
            $validate = $this->validate($data, $rule);
            if ($validate !== true) {
                return $this->errorReturn(SystemException::E_VALIDATE, $validate);
            }
            try {
                $userInfo = $this->auth->getUser();
                if (!$userInfo) {
                    exception(UserException::getErrorMsg(UserException::E_PASSWORD), UserException::E_PASSWORD, $this->_exceptions['user']);
                }
                $memberModel = model('common/Members');
                $res = $memberModel->where(['member_id' => $this->auth->member_id])->update(['phone' => $data['phone']]);
            } catch (UserException $userE) {
                return $this->errorReturn($userE->getCode(), $userE->getMessage());
            }
            return $this->successReturn($res, SystemException::E_SUCCESS, '修改成功');
        }
        return $this->successReturn(['phone'=> $this->auth->phone], SystemException::E_SUCCESS, '获取成功');
    }

}